package nccloud.mob.riart.message.actions;

import com.alibaba.fastjson.JSONObject;

import nc.impl.pubapp.pattern.database.DataAccessUtils;
import nc.md.data.access.NCObject;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pubapp.pattern.data.IRowSet;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.mob.action.itf.IMobileAction;
import nccloud.framework.mob.container.IMobRequest;
import nccloud.framework.mob.container.MobClientInfo;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.utils.riart.MessageLogger;
import nccloud.web.arap.arappub.util.ArapBillUIUtil;


public class QueryRecBillDetaiAction implements IMobileAction {

	@Override
	public Object doAction(IMobRequest request) {
		try {
			String str = request.read();
			JSONObject json = JSONObject.parseObject(str);		
			String appcode = (String) json.get("appCode");
			String pk = (String) json.get("pk_bill");
			String pagecode = (String) json.get("pageId");
			String billtype = (String) json.get("billtype");
			MobClientInfo mobClientInfo = new MobClientInfo();
			mobClientInfo.setAppCode(appcode);
			String[] metaInfo = getMetaInfoByBilltype(billtype);
			String aggVoClass = metaInfo[1];
			Class clazzAgg = Class.forName(aggVoClass);
			//取数先不考虑组织权限
			IMDPersistenceQueryService service = ServiceLocator.find(IMDPersistenceQueryService.class);
			Object billvo = null;
			NCObject ncObj = service.queryBillOfNCObjectByPKWithDR(clazzAgg, pk, true);
			if(ncObj != null){
				billvo = ncObj.getContainmentObject(); 
				if(billvo instanceof BaseAggVO){
					ArapBillUIUtil.refreshChildVO2HeadVO( new AggregatedValueObject[]{(AggregatedValueObject) billvo});
				}
				BillCardOperator operator = new BillCardOperator(pagecode);
				operator.setTransFlag(false);//设置隐藏字段不翻译
				BillCard billCard = operator.toCard(billvo);
				return billCard;
			}
		} catch (Exception e) {
			MessageLogger.error("操作此类【nccloud.mob.riart.message.actions.QueryRecBillDetaiAction】是出现错误--审批单据详情查询异常！，错误描述：【"+e.getMessage()+"】");
			ExceptionUtils.wrapBusinessException("审批单据详情查询异常！" + e.getMessage());
		} 
		return null;
	}
	/**
	 * 根据单据类型取到主表名称以及聚合VO的类型名称
	 * @param billtype
	 * @return
	 */
	public String[] getMetaInfoByBilltype(String billtype){
		
		String strSql = "SELECT a.defaulttablename,a.fullclassname,p.paravalue,a.id FROM" +
			" md_accessorpara p LEFT JOIN md_class a ON p.id = a.id LEFT JOIN md_component b ON a.componentid = b.id" + 
	    	" LEFT JOIN bd_billtype c ON b.name = c.component WHERE a.isprimary = 'Y' and c.pk_billtypecode = " ;
	    strSql = strSql + "'" + billtype + "'";
	   
		DataAccessUtils dao = new DataAccessUtils();
		IRowSet rowset = dao.query(strSql.toString());
		
		String[][] result = new String[1][3];
		if(rowset != null && rowset.size() > 0){
			result = rowset.toTwoDimensionStringArray();
		}
		return result[0];
	}
}
